from PyQt5.QtCore import QObject,QThread,pyqtSignal
from PyQt5.QtWidgets import QMainWindow, QMessageBox
import time

from Utils.ui.QT_MainWindow import Ui_MainWindow  # 导入Ui_MainWindow类(QT设计师创建)
from Utils.Screen import screen   # 导入 screen 对象(python代码)
from Utils.log import logger,mylog  # 导入 logger 对象(python代码)
# from Utils.User_Import import *  # 导入 myWin, screen, logger 对象(python代码)

#QT线程类
class MyThread(QObject):

    updata_date = pyqtSignal(str)  # 定义信号 (更新messagebox信息)
    check_msg = pyqtSignal(str)  # 定义信号 (检查messagebox状态)

    def __init__(self,parent=None):
        super(MyThread, self).__init__(parent)  # 初始化父类

        self.thread = QThread()
        self.moveToThread(self.thread)
        if mylog.log_enabled is True:
            logger.info('线程创建成功')
        self.updata_date.connect(self.Update_MousePosition_information)
        self.check_msg.connect(self.Ckeck_MousePosition_information_State)

    def Start(self):
        self.thread.started.connect(self.run)
        self.thread.start()
        if mylog.log_enabled is True:
            logger.info('线程启动')
    def Stop(self):
        self.thread.requestInterruption()  # 请求中断
        self.thread.quit()
        self.thread.wait()
        if mylog.log_enabled is True:
            logger.info('线程结束')

    def run(self):
        while not self.thread.isInterruptionRequested():
            self.check_msg.emit('')
            self.updata_date.emit(f'当前鼠标位置在：{screen.Mouse_Position_x,screen.Mouse_Position_y}')
            time.sleep(0.1)

    #更新MassageBox信息(鼠标位置)
    def Update_MousePosition_information(self, data):
        screen.get_Mouse_Position()
        from Utils.MainWindow import myWin  # 导入 myWin 对象(python代码)
        myWin.MousePosition_information_msg.setText(data)

    #检测MassageBox状态(鼠标位置)
    def Ckeck_MousePosition_information_State(self):
        from Utils.MainWindow import myWin  # 导入 myWin 对象(python代码)
        if myWin.MousePosition_information_msg.isVisible() is False:#判断是否显示
            mythread.Stop()  # 停止线程

mythread = MyThread()  # 实例化 MyThread 类